package py.com.alfa.escriba.gui.rest.services.resttemplate;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import py.com.alfa.escriba.gui.config.EscribaConfigApplicationContextProvider;
import py.com.alfa.escriba.gui.dto.gestion.VariablesDTO;
import py.com.alfa.escriba.gui.rest.services.VariablesService;
import py.com.alfa.escriba.gui.services.Services;
import py.com.sif.rest.client.spring.request.ClientRequestBuilder;

@SuppressWarnings({ "unchecked"})
public class VariablesServiceClient implements VariablesService {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	public VariablesServiceClient() {}

	@Override
	public List<VariablesDTO> listVariables() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<VariablesDTO[]> response = (ResponseEntity<VariablesDTO[]>) request.exchange(LIST_VARIABLE, HttpMethod.GET, VariablesDTO[].class);
		log.info("OK - listVariables ");
		List<VariablesDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public VariablesDTO createVariables(VariablesDTO variable) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(variable);
		ResponseEntity<VariablesDTO> response = (ResponseEntity<VariablesDTO>) request.exchange(LIST_VARIABLE, HttpMethod.POST, VariablesDTO.class);
		log.info("OK - createTemplate ");
		VariablesDTO result = response.getBody();
		return result;
	}

	@Override
	public VariablesDTO saveVariables(VariablesDTO variable) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", variable.getId().toString());
		request.addBody(variable);
		ResponseEntity<VariablesDTO> response = (ResponseEntity<VariablesDTO>) request.exchange(MB_VARIABLE, HttpMethod.PUT, VariablesDTO.class);
		log.info("OK - saveTemplate ");
		VariablesDTO result = response.getBody();
		return result;
	}

	@Override
	public void deleteVariables(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(MB_VARIABLE, HttpMethod.DELETE, null);
		log.info("OK - deleteTemplate ");

	}

	@Override
	public VariablesDTO showVariables(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<VariablesDTO> response = (ResponseEntity<VariablesDTO>) request.exchange(MB_VARIABLE, HttpMethod.GET, VariablesDTO.class);
		log.info("OK - showTemplate ");
		VariablesDTO result = response.getBody();
		return result;
	}

	@Override
	public List<VariablesDTO> listVariablesByIdTipo(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<VariablesDTO[]> response = (ResponseEntity<VariablesDTO[]>) request.exchange(VARIABLE_BY_TIPO, HttpMethod.GET, VariablesDTO[].class);
		log.info("OK - listVariables ");
		List<VariablesDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

}
